<!-- 农场产品邮寄记录 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row type="flex" justify="space-between" :gutter="48">
                    <a-col :md="8">
                        <a-form-item label="渠道标识">
                            <a-input v-model="channelTag" allow-clear placeholder="请输入渠道标识" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8">
                        <a-form-item label="订单类型">
                            <a-select v-model="orderType" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in orderTypeList" :key="i" :value="item.orderType">{{item.description}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="日期">
                            <a-range-picker v-model="dateValue" @change="dateChange"/>
                        </a-form-item>
                    </a-col>
                    <a-col :md="16">
                    </a-col>
                    <a-col :md="8">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getData()">查询</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small">
            <div slot="channelTag" slot-scope="channelTag">
                <div v-for="(item,i) in channelList" :key="item.id + 'i'">
                    <a-tag v-if="item.channelTag == channelTag" :color="colors[i%13]">{{item.channelName}}</a-tag>
                </div>
            </div>
            <div slot="gtDates">
                <div>{{gtDate}}</div>
            </div>
            <div slot="ltDates">
                <div>{{ltDate}}</div>
            </div>
        </a-table>
    </div>
</template>

<script>
import { columns } from './table';
import { queryOrderFormDailyStat , queryTransactionType } from '@/api/order';
import { queryBrandChannelConfig } from '@/api/brand';
import moment from 'moment';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            isload: false,
            visible: false,
            dataList: [],
            total: 0,
            columns,
            phone: '', // 手机号
            channelTag: '', // 渠道标识
            orderType: undefined,
            orderTypeList: [],
            channelList: [],
            page: 1,
            size: 10,
            confirmLoading: false,
            drawerVisible: false,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            dateValue: [moment('2022-08-01', 'YYYY-MM-DD'),moment().format('YYYY-MM-DD')],
            gtDate: moment('2022-08-01', 'YYYY-MM-DD').format('YYYY-MM-DD'),
            ltDate: moment().format('YYYY-MM-DD'),
        };
    },
    async created(){
        try {
            this.getType()
            this.getChannel()
            // this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
    methods: {
        moment,
        // 查询所有通道
        getChannel(){
            let _this = this
            const params = {
                'size':99,
                'page':0,
            }
            queryBrandChannelConfig(params)
            .then(res=>{
                if(res.success){
                    _this.channelList = res.data.content
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 查询订单类型
        getType(){
            let _this = this
            const params = {

            }
            queryTransactionType(params)
            .then(res=>{
                if(res.success){
                    _this.orderTypeList = res.data
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        async getData(){
            let _this = this
            _this.isload = true
            _this.dataList = []
            console.log(_this.gtDate,_this.ltDate,'gtDate')
            for(let i = 0; i < _this.channelList.length; i++){
                console.log('channelTag')
                if(_this.channelTag == '' || _this.channelTag == _this.channelList[i].channelTag){
                    var channelTag = _this.channelTag == '' ? _this.channelList[i].channelTag : _this.channelTag;
                    console.log(channelTag,'channelTag')
                    var data = await _this.proData(channelTag);
                    _this.dataList.push(data);
                }
            }
            _this.isload = false;
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            // this.getData()
        },
        del(item){
            let _this = this
            const params = {
                'id':item.id,
            }
            updateFarmerPostRecord(params)
            .then(res=>{
                if(res.success){
                    _this.isUpdate = false
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // promise获取数据
        async proData(channelTag){
            let _this = this;
            return new Promise((resolve,reject) =>{
                const params = {
                    'channelTag': channelTag,
                    'orderType': _this.orderType,
                    'gtDate': _this.gtDate,
                    'ltDate': _this.ltDate,
                }
                queryOrderFormDailyStat(params)
                .then(res=>{
                    if(res.success){
                        resolve(res.data)
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        dateChange(date,dateString){
            this.gtDate = dateString[0];
            this.ltDate = dateString[1];
            this.getData();
        },
    }
}

</script>
<style scoped>

</style>